package com.chaopei.day10.review;

import java.io.*;
import java.net.Socket;

/**
 * @Author Corday 天涯
 * @Date 2022/3/16 9:18
 */
public class MyClient {
    public static void main(String[] args) {
        //服务端启动后,再启动此处的客户端
        play();
    }//main

    public static void play() {
//  网络字符输出流:PrintWrite的应用
        InputStream in = null;
        OutputStream out = null;
        //输出流
        PrintWriter pw = null;
        Socket socket = null;
        BufferedReader br = null;
//       创建各种对象
        try {
            socket = new Socket("127.0.0.1", 9080);
            in = socket.getInputStream();
            out = socket.getOutputStream();
            //直接将字节流转成了字符流
            pw = new PrintWriter(out, true);
            //输出数据到控制台或网络
            pw.println("我是客户,你在吗?");
            //获取返回的消息
            br = new BufferedReader(new InputStreamReader(in));
            String msg = br.readLine();
            if(msg!=null){
                System.out.println("收到服务端:"+msg);
            }else{
                System.out.println("服务端无响应!");
            }
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
